<template>
  <div class="sidebar">
    <ul class="nav nav-sidebar">
      <template v-for="(row, index) in parentList">
        <li class="nav-parent"
            v-if="row.parentCode=='menu_0000' &&findSubNode(row.resourcesCode)">
          <router-link v-bind:to="row.resourcesHref"
                       class="nav-parent-title">
            <i v-bind:class="row.resourcesIcon"></i> {{row.resourcesName}}
            <i class="fa fa-angle-down iconRight"></i>
          </router-link>
        </li>
        <li class="nav-parent"
            v-if="row.parentCode=='menu_0000' &&!findSubNode(row.resourcesCode)">
          <a class="nav-parent-title">
            <i v-bind:class="row.resourcesIcon"></i>
            {{row.resourcesName}}
            <i class="fa fa-angle-down iconRight"></i>
          </a>
          <ul class="nav nav-children"
              v-for="(node,index) in subNodeList">
            <li v-if="node.parentCode==row.resourcesCode">
              <router-link v-bind:to="node.resourcesHref"><i class="fa fa-circle iconLeft"></i>{{node.resourcesName}}</router-link>
            </li>
          </ul>
        </li>
      </template>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      menuInfo: {
        resourcesType: "1",
        resourcesOrigin: "2",
        resourcesLevel: "0"
      },
      parentList: [],
      subNodeList: []
    };
  },
  mounted: function () {
    //初始化
    this.initMenu();
  },
  methods: {
    initMenu: function () {
      var _self = this;
      var body = {};
      var params = _self.menuInfo;
      body.params = params;
      var url = _self.utilHelper.apiUrl + "/api/getMenu";
      _self.$http.post(url, body).then((response) => {
        if (response.body.resData != null) {
          _self.parentList = response.body.resData.resourcesMap.parent;
          _self.subNodeList = response.body.resData.resourcesMap.subNode;
          _self.$router.replace({
            path: "/index/"
          });
        } else {
          window.localStorage.setItem("isLogin", false);
          _self.$router.replace({
            path: "/login"
          });
        }
      });
    },
    findSubNode: function (resourcesCode_p) {
      var _self = this;
      var count = 0;
      var subNodeList = this.subNodeList;
      for (var i = 0; i < subNodeList.length; i++) {
        if (subNodeList[i].parentCode == resourcesCode_p) {
          count++;
        }
      }
      if (count <= 0) {
        return true;
      } else {
        return false;
      }
    }
  }
};
</script>